package com.itheima.core.web.controller;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itheima.common.utils.Page;

import com.itheima.core.po.User;

import com.itheima.core.service.UserService;
/**
 * 用户控制器类
 */
@Controller
public class UserController {
	// 依赖注入
	@Autowired
	private UserService userService;
	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String userId,String password,String type, Model model, 
                                                          HttpSession session,HttpServletRequest request) {
		// 通过账号和密码查询用户
		User user = userService.findUser(userId, password, type);
		if(user != null && "管理员".equals(type)){		
			// 将用户对象添加到Session
			session.setAttribute("USER_SESSION", user);
			// 跳转到主页面
//			return "user";
			return "MyJsp";
		}if(user != null && "员工".equals(type)){		
			// 将用户对象添加到Session
			session.setAttribute("USER_SESSION", user);
			// 跳转到主页面
//			return "user";
			return "member";
		}
		
		model.addAttribute("msg", "账号或密码错误，请重新输入！");
         // 返回到登录页面
		return "login";
	}
	
	/**
	 * 模拟其他类中跳转到客户管理页面的方法
	 */
	@RequestMapping(value = "/toUser.action")
	public String toUser() {
	    return "user";
	}
	
	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
	    // 清除Session
	    session.invalidate();
	    // 重定向到登录页面的跳转方法
	    return "redirect:login.action";
	}
	/**
	 * 向用户登陆页面跳转
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String toLogin() {
	    return "login";
	}


	
	
	

		// 客户来源

		/**
		 *  客户列表
		 */
		@RequestMapping(value = "/user/list.action")
		public String list(@RequestParam(defaultValue="1")Integer page,
				@RequestParam(defaultValue="10")Integer rows, 
				String userId, String password, String fullName,
				String type, Model model) {
			// 条件查询所有客户
			Page<User> users = userService
					.findUserList(page, rows,userId, 
							password, fullName,type);
			model.addAttribute("page", users);
			// 客户来源
			model.addAttribute("userId", userId);
			model.addAttribute("password", password);
			model.addAttribute("fullName", fullName);
			model.addAttribute("type", type);
			return "./user/user";
			
		}
		
		
		
		@RequestMapping(value ="/user/create.action")
		public String add() {
		    // 获取Session中的当前用户信息
		    return "createUser";
		    }
		
	/*	@RequestMapping(value ="/user/craeteUser.action")
		@ResponseBody
		public String createUser(User user) {
			userService.createUser(user);
		    return "redirect:list.action";
		    }*/
		@RequestMapping("/user/createUser.action")
		public String userCreate(User user,HttpSession session) {
		    // 获取Session中的当前用户信息
		    User user1 = (User) session.getAttribute("USER_SESSION");
		    // 将当前用户id存储在客户对象中
		    user.setUserId(user.getUserId());
		    // 创建Date对象
		    Date date = new Date();
		    // 执行Service层中的创建方法，返回的是受影响的行数
		    int rows = userService.createUser(user);
		    if(rows > 0){
		        
		    }
		    return "redirect:list.action";
		}

		@RequestMapping("/user/getUserById.action")
		public String getUserById(String userId,Model model) {
//			System.out.println(id);
		    User user = userService.getUserById(userId);
		    model.addAttribute("user", user);
//		    System.out.println(user);
		    return "updateUser";
		}
		
		
		/**
		 * 更新客户
		 */
		@RequestMapping(value ="/user/update.action")
		public String update() {
		    // 获取Session中的当前用户信息
		    return "updateUser";
		    }
		
		@RequestMapping("/user/updateUser.action")
		public String userUpdate(User user) {
		    int rows = userService.updateUser(user);
		    if(rows > 0){
		    }
		    return "redirect:list.action";
		}

		/**
		 * 删除客户
		 */
		@RequestMapping("/user/delete.action")
		public String userDelete(String userId) {
		    userService.deleteUser(userId);
		    return "redirect:list.action";
		}
}

